﻿using Sgml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace xFreeHuUrlGetter
{
    class Program
    {
        static void Main(string[] args) {
            try {
                string saveDir = "d:\\画像\\xfree.hu\\A legszebb szexi csajok";
                string infoFilePath = Path.Combine(saveDir, string.Format("info{0}.txt", DateTime.Now.ToString("yyMMddHHmmss")));
                Encoding enc = Encoding.GetEncoding("utf-8");
                using (StreamWriter infoWriter = new StreamWriter(infoFilePath, true, enc)) {
                    string nextUrl = "http://xfree.hu/kep_show.tvn?kid=214189&aid=222876&sort=1";
                    int prevNo = 0;
                    while (true) {
                        PageInfo pageInfo = getPageInfo(nextUrl);
                        if (prevNo > pageInfo.No)
                            break;
                        infoWriter.WriteLine(string.Format("{0}\t{1}\t{2}", pageInfo.ImageUrl, pageInfo.No, pageInfo.ImageTitle));
                        Console.WriteLine(string.Format("  -> {0}\t{1}", pageInfo.No, pageInfo.ImageTitle));

                        downloadImage(pageInfo, saveDir);
                        nextUrl = pageInfo.NextUrl;
                        prevNo = pageInfo.No;
                    }
                }
            }
            catch (Exception exp) {
                Console.WriteLine(exp.ToString());
            }
            Console.WriteLine("終了");
            Console.ReadKey();
        }

        class PageInfo
        {
            public string ImageUrl;
            public string ImageTitle;
            public string PrevUrl;
            public string NextUrl;
            public int No;
        }

        static PageInfo getPageInfo(string url) {
            // url = http://xfree.hu/kep_show.tvn?kid=214189&aid=222876&sort=1

            Console.WriteLine("★" + url);

            WebRequest req = WebRequest.Create(url);
            WebResponse res = req.GetResponse();

            HttpWebResponse httpRes = res as HttpWebResponse;
            if (httpRes.StatusCode != HttpStatusCode.OK) {
                Console.WriteLine("statusCode = " + httpRes.StatusCode);
                return null;
            }

            Encoding enc = Encoding.GetEncoding("utf-8");
            string src;

            using (Stream st = res.GetResponseStream())
            using (StreamReader sr = new StreamReader(st, enc)) {
                src = sr.ReadToEnd();
            }

            XmlDocument xmlDoc = new XmlDocument();
            using (SgmlReader sgmlReader = new SgmlReader()) {
                StringReader strReader = new StringReader(src);

                // read response
                sgmlReader.DocType = "HTML";
                //tReader.WhitespaceHandling = WhitespaceHandling.All;
                //tReader.CaseFolding = Sgml.CaseFolding.ToLower;
                sgmlReader.InputStream = strReader;

                //tDocument.PreserveWhitespace = true;
                //tDocument.XmlResolver = null;
                xmlDoc.Load(sgmlReader);
            }

            //xmlDoc.Save("test.xml");

            PageInfo pageInfo = new PageInfo();

            // ダウンロードする画像
            // xpath: /html/body/center/a
            // <a title="Mona candle light 000.jpg" target="_blank" href="http://s4.images.www.tvn.hu/2011/05/29/00/02/www.tvn.hu_080ab21dfdd54eceeffb86456124f418.jpg">
            XmlNode elem = xmlDoc.SelectSingleNode("/html/body/center/a");
            pageInfo.ImageUrl = elem.Attributes["href"].Value;
            pageInfo.ImageTitle = elem.Attributes["title"].Value;

            // 前の画像のページ
            // xpath: /html/body/center/table/tbody/tr/td[2]/table/tbody/tr/td/a[2]
            // <a title="Előző kép" href="kep_show.tvn?aid=222876&amp;kid=552015&amp;sort=1">
            elem = xmlDoc.SelectSingleNode("/html/body/center/table/tr/td[2]/table/tr/td/a[2]");
            string prevUrl = elem.Attributes["href"].Value;
            pageInfo.PrevUrl = new Uri(new Uri(url), prevUrl).AbsoluteUri;

            // 次の画像のページ
            // xpath: /html/body/center/table/tbody/tr/td[2]/table/tbody/tr/td/a[3]
            // <a title="Következő kép" href="kep_show.tvn?aid=222876&amp;kid=214190&amp;sort=1">
            elem = xmlDoc.SelectSingleNode("/html/body/center/table/tr/td[2]/table/tr/td/a[3]");
            string nextUrl = elem.Attributes["href"].Value;
            pageInfo.NextUrl = new Uri(new Uri(url), nextUrl).AbsoluteUri;

            // ページ番号
            // xpath: /html/body/center/table/tbody/tr/td[2]/table/tbody/tr/td/font/b
            // <b>1</b>
            elem = xmlDoc.SelectSingleNode("/html/body/center/table/tr/td[2]/table/tr/td/font/b");
            pageInfo.No = int.Parse(elem.InnerText);

            return pageInfo;
        }

        static void downloadImage(PageInfo pageInfo, string dir) {
            // 保存ファイルパスを作成
            string title = pageInfo.ImageTitle;
            title.Trim();
            char[] invalidChar = Path.GetInvalidFileNameChars();
            foreach (char c in invalidChar) {
                title = title.Replace(c, '_');
            }

            string ext = Path.GetExtension(pageInfo.ImageUrl);
            if (string.IsNullOrEmpty(ext))
                throw new Exception("urlから拡張子を取得するのに失敗");

            // ダウンロードするurlの拡張子がタイトルのお尻についている場合は追加しない。
            if (!title.EndsWith(ext, StringComparison.OrdinalIgnoreCase))
                title += ext;

            string filePath = Path.Combine(dir, string.Format("{0} - {1}", pageInfo.No, title));

            // 存在している場合、何もしない
            if (File.Exists(filePath))
                return;

            //WebRequestの作成
            HttpWebRequest webreq = (HttpWebRequest)WebRequest.Create(pageInfo.ImageUrl);

            //サーバーからの応答を受信するためのWebResponseを取得
            HttpWebResponse webres = (HttpWebResponse)webreq.GetResponse();

            // メモリに格納
            MemoryStream ms = new MemoryStream();

            //応答データを受信するためのStreamを取得
            using (Stream rs = webres.GetResponseStream()) {
                byte[] buffer = new byte[65535];
                while (true) {
                    int rb = rs.Read(buffer, 0, buffer.Length);
                    if (rb > 0) {
                        ms.Write(buffer, 0, rb);
                    }
                    else {
                        break;
                    }
                }
            }

            // ファイルに保存
            FileStream fs = new FileStream(filePath, FileMode.Create);
            fs.Write(ms.GetBuffer(), 0, (int)ms.Length);
            fs.Close();
        }


    }
}
